﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.ModFlow.Domain.DomainLayer;

using System.Collections.ObjectModel;

using HA.ModFlow.Controls.UserControls;

namespace HA.ModFlow.Controls.Windows
{
    /// <summary>
    /// winSourcesSinks.xaml 的交互逻辑
    /// </summary>
    public partial class winSourcesSinks : Window
    {
        public winSourcesSinks(ModFlowData mfData, List<int> cellsIndex)
        {
            InitializeComponent();
            //this.dtGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
            this.ModFlowData = mfData;

            this._wells = new ObservableCollection<Well>();
            this._river = new ObservableCollection<River>();
            this._Drain = new ObservableCollection<Drain>();
            this._GeneralHead = new ObservableCollection<GeneralHead>();
            this._specifiedHead = new ObservableCollection<SpecifiedHead>();


            this._indices = cellsIndex;

            this._nrows = this.ModFlowData.GridData.RowsCount;

            this._ncols = this.ModFlowData.GridData.ColsCount;

            this._nlayers = this.ModFlowData.GridData.LayersCount;


            this._nPeriods = this.ModFlowData.StressPeriods.InnerList.Count;




            this.GenerateRiveColumns();
            this.GenerateDrainColumns();
            this.GenerateWellColumns();
            this.GenerateGeneralHeadColumns();

            this.GenerateSpecifiedHeadColumns();

            ucMultiInput.mainWindow = this;


            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.dtGrid.ContextMenu = cm;
        }

        private ModFlowData _ModFlowData;

        public ModFlowData ModFlowData
        {
            get { return _ModFlowData; }
            set { _ModFlowData = value; }
        }

        int _nrows;
        int _ncols;
        int _nlayers;

        int _nPeriods;

        List<int> _indices;

        string _currentBCString;


        //动态列的代码写法
        ObservableCollection<DataGridColumn> _riverColumns=new ObservableCollection<DataGridColumn> ();
        ObservableCollection<DataGridColumn> _WellsColumns = new ObservableCollection<DataGridColumn>();
        ObservableCollection<DataGridColumn> _WellMNWColumns = new ObservableCollection<DataGridColumn>();
        ObservableCollection<DataGridColumn> _SpecifiedheadColumns = new ObservableCollection<DataGridColumn>();
        ObservableCollection<DataGridColumn> _DrainColumns = new ObservableCollection<DataGridColumn>();
        ObservableCollection<DataGridColumn> _DrainDTRColumns = new ObservableCollection<DataGridColumn>();
        ObservableCollection<DataGridColumn> _GeneralHeadColumns = new ObservableCollection<DataGridColumn>();



        ObservableCollection<Well> _wells;
        ObservableCollection<River> _river;

        ObservableCollection<Drain> _Drain;
        ObservableCollection<GeneralHead> _GeneralHead;

        ObservableCollection<SpecifiedHead> _specifiedHead;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            
            //foreach (Well item in this.ModFlowData.SourceSinks.Wells.Wells)
            //{
            //    this._wells.Add((Well)item.Clone());

                
            //}
         

            foreach (int item in this._indices)
            {

                List<River> rivers = this.ModFlowData.SourceSinks.Rivers.GetRiver(item);
                foreach (River river in rivers)
                {

                    this._river.Add((River)river.Clone());

                }



                List<Well> wells = this.ModFlowData.SourceSinks.Wells.GetWell(item);
                foreach (Well wel in wells)
                {
                    this._wells.Add((Well)wel.Clone());
                }




                List<Drain> drain = this.ModFlowData.SourceSinks.Drains.GetDrain(item);
                foreach (Drain rain in drain)
                {
                    this._Drain.Add((Drain)rain.Clone());
                }



                List<GeneralHead> Genreal = this.ModFlowData.SourceSinks.GeneralHeads.GetGHB(item);
                foreach (GeneralHead gen in Genreal)
                {
                    this._GeneralHead.Add((GeneralHead) gen.Clone());
                }


                List<SpecifiedHead> spHead = this.ModFlowData.SourceSinks.SpecifiedHeads.GetSpecifiedHead(item);
                foreach (SpecifiedHead head in spHead)
                {
                    this._specifiedHead.Add((SpecifiedHead)head);

                }



            }

        



            //初始化UI条目

            ListBoxItem lbi;

            lbi = new ListBoxItem();
            lbi.Content = "井";
            lbi.Tag = "Wells";

            this.lstSC.Items.Add(lbi);


            lbi = new ListBoxItem();
            lbi.Content = "河流";

            lbi.Tag = "River";

            this.lstSC.Items.Add(lbi);


            lbi = new ListBoxItem();
            lbi.Content = "通用水头";

            lbi.Tag = "GeneralHead";

            this.lstSC.Items.Add(lbi);


            lbi = new ListBoxItem();
            lbi.Content = "排水渠";

            lbi.Tag = "Drain";

            this.lstSC.Items.Add(lbi);



            lbi = new ListBoxItem();
            lbi.Content = "时变水头";

            lbi.Tag = "SpecifiedHead";

            this.lstSC.Items.Add(lbi);



            //需要写一个查找算法，初始的情况就跳转到对应的边栏

            //开始构建事件体系

            this.lstSC.SelectedIndex = 0;

            foreach (int item in this._indices)
            {
                foreach (Well well in this._wells)
                {
                    if (well.Index==item)
                    {
                        this.lstSC.SelectedIndex = 0;

                        return;
                        
                    }
                    
                }

                foreach (River river in this._river)
                {
                    if (river.Index==item)
                    {
                        this.lstSC.SelectedIndex = 1;

                        return;
                        
                    }

                    
                }


                foreach (GeneralHead river in this._GeneralHead)
                {
                    if (river.Index == item)
                    {
                        this.lstSC.SelectedIndex =2;

                        return;

                    }


                }

                foreach (Drain river in this._Drain)
                {
                    if (river.Index == item)
                    {
                        this.lstSC.SelectedIndex = 3;

                        return;

                    }


                }


                foreach (SpecifiedHead river in this._specifiedHead)
                {
                    if (river.Index == item)
                    {
                        this.lstSC.SelectedIndex = 4;

                        return;

                    }


                }

                
            }


          

           



        }



        #region 内部函数



        protected void GenerateRiveColumns()
        {    
            GetIDIJK(this._riverColumns);

            DataGridTemplateColumn column = new DataGridTemplateColumn();
            column.Header = "水头[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length) + "]";
            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            Binding binding = new Binding();
            binding.Path = new PropertyPath("Stage");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._riverColumns.Add(column);

            column = new DataGridTemplateColumn();
            column.Header = "导水系数[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length) + "^2/" + Domain.DomainLayer.UnitConverter.GetTime(ModFlowData.ModelOption.Units.Time) + "]";
            dt = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            binding = new Binding();
            binding.Path = new PropertyPath("Conductance");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._riverColumns.Add(column);


            column = new DataGridTemplateColumn();
            column.Header = "海拔[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length) + "]";
            dt = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            binding = new Binding();
            binding.Path = new PropertyPath("Elevation");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._riverColumns.Add(column);
        }


        protected void GetIDIJK(ObservableCollection<DataGridColumn> comColumn)
        {
           
            DataGridTextColumn txt = new DataGridTextColumn();
            txt.Header = "ID";
            txt.IsReadOnly = true;
            Binding bin = new Binding("Index");
            txt.Binding = bin;
            comColumn.Add(txt);

            txt = new DataGridTextColumn();
            txt.Header = "行";
            txt.IsReadOnly = true;
            bin = new Binding("I");          
            txt.Binding = bin;
            comColumn.Add(txt);

            txt = new DataGridTextColumn();
            txt.Header = "列";
            txt.IsReadOnly = true;
            bin = new Binding("J");
            txt.Binding = bin;
            comColumn.Add(txt);

            txt = new DataGridTextColumn();
            txt.Header = "层";
            txt.IsReadOnly = true;
            bin = new Binding("K");
            txt.Binding = bin;
            comColumn.Add(txt);
        }
       

        /// <summary>
        /// Well
        /// </summary>
        protected void GenerateWellColumns()
        {            
            GetIDIJK(this._WellsColumns);

            DataGridTemplateColumn column = new DataGridTemplateColumn();
            column.Header = "流速(flow)[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length) + "^3/" + Domain.DomainLayer.UnitConverter.GetTime(ModFlowData.ModelOption.Units.Time) + "]";
            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            Binding binding = new Binding();
            binding.Path = new PropertyPath("RechargeRates");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._WellsColumns.Add(column);
        }

        /// <summary>
        /// WellMNW
        /// </summary>
        protected void GenerateWellMNWColumns()
        {
            DataGridTextColumn dt = new DataGridTextColumn();
            dt.Header = "ID";
            Binding bin = new Binding("Index");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "名称";
            bin = new Binding("FistName");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "激活";
            bin = new Binding("FirstActive");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "流速(flow)[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length) + "^3/" + Domain.DomainLayer.UnitConverter.GetTime(ModFlowData.ModelOption.Units.Time) + "]";
            bin = new Binding("FirstQdes");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "井编号";
            bin = new Binding("FirstWellid");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Qwval";
            bin = new Binding("FirstQwval");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Rw";
            bin = new Binding("FirstRw");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Skin";
            bin = new Binding("FirstSkin");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Hlin";
            bin = new Binding("FirstHlin");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Href";
            bin = new Binding("FirstHref");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "DD";
            bin = new Binding("FirstDD");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Iqw grp";
            bin = new Binding("FirstIqwgrp");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Cp:C";
            bin = new Binding("FirstCpc");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "QCUT";
            bin = new Binding("FistQcut");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Qfrcmn";
            bin = new Binding("FirstQfrcmn");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Qfrcmx";
            bin = new Binding("FirstQfrcmx");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Print Site";
            bin = new Binding("FirstPrintSite");
            dt.Binding = bin;

            this._WellMNWColumns.Add(dt);
        }
        /// <summary>
        /// SpecifiedHead
        /// </summary>
        protected void GenerateSpecifiedHeadColumns()
        {
            GetIDIJK(this._SpecifiedheadColumns);

            DataGridTemplateColumn column = new DataGridTemplateColumn();
            column.Header = "水头-应力期[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length)+"]";
            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            Binding binding = new Binding();
            binding.Path = new PropertyPath("SHead");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._SpecifiedheadColumns.Add(column);

            column = new DataGridTemplateColumn();
            column.Header = "水头-应力期[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length)+"]";
            dt = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            binding = new Binding();
            binding.Path = new PropertyPath("EHead");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._SpecifiedheadColumns.Add(column);

        }
        /// <summary>
        /// Drain
        /// </summary>
        protected void GenerateDrainColumns()
        {
            GetIDIJK(this._DrainColumns);

            DataGridTemplateColumn column = new DataGridTemplateColumn();
            column.Header = "底部海拔[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length)+"]";
            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            Binding binding = new Binding();
            binding.Path = new PropertyPath("Elevation");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._DrainColumns.Add(column);

            column = new DataGridTemplateColumn();
            column.Header = "界面导水系数[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length) + "^2/" + Domain.DomainLayer.UnitConverter.GetTime(ModFlowData.ModelOption.Units.Time) + "]";
            dt = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            binding = new Binding();
            binding.Path = new PropertyPath("Conductance");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._DrainColumns.Add(column);         
        }
        /// <summary>
        /// DrainDRT
        /// </summary>
        protected void GenerateDrainDRTColumns()
        {
            DataGridTextColumn dt = new DataGridTextColumn();
            dt.Header = "编号";
            Binding bin = new Binding("Index");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "底部海拔[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length) + "]";
            bin = new Binding("FirstElevation");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "导水系数[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length) + "^2/" + Domain.DomainLayer.UnitConverter.GetTime(ModFlowData.ModelOption.Units.Time) + "]";
            bin = new Binding("FirstConductance");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Return 层";
            bin = new Binding("FirstReturnK");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Retrun 行";
            bin = new Binding("FirstReturnI");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Return 列";
            bin = new Binding("FirstRetrunJ");
            dt.Binding = bin;

            dt = new DataGridTextColumn();
            dt.Header = "Rfprop";
            bin = new Binding("FirstRfprop");
            dt.Binding = bin;

            this._DrainDTRColumns.Add(dt);
        }
        /// <summary>
        /// GeneralHead
        /// </summary>
        protected void GenerateGeneralHeadColumns()
        {
            GetIDIJK(this._GeneralHeadColumns);

            DataGridTemplateColumn column = new DataGridTemplateColumn();
            column.Header = "水头-应力期[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length)+"]";
            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            Binding binding = new Binding();
            binding.Path = new PropertyPath("Head");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._GeneralHeadColumns.Add(column);

            column = new DataGridTemplateColumn();
            column.Header = "导水系数[" + Domain.DomainLayer.UnitConverter.GetLength(ModFlowData.ModelOption.Units.Length) + "^2/" + Domain.DomainLayer.UnitConverter.GetTime(ModFlowData.ModelOption.Units.Time) + "]";
            dt = new DataTemplate();
            fef = new FrameworkElementFactory(typeof(HA.ModFlow.Controls.UserControls.ucMultiInput));
            binding = new Binding();
            binding.Path = new PropertyPath("Conductance");
            fef.SetBinding(ucMultiInput.DataContextProperty, binding);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            this._GeneralHeadColumns.Add(column);
        }

        #endregion





        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBoxItem lbi =(ListBoxItem) this.lstSC.SelectedItem;

            if (lbi!=null)
            {
                string str = (string)lbi.Tag;

                this._currentBCString = str;

            

                switch (this._currentBCString)
                {
                    case "River":

                       

                        this.dtGrid.Columns.Clear();

                        foreach (DataGridColumn item in this._riverColumns)
                        {
                            this.dtGrid.Columns.Add(item);
                            
                        }

                        
                        this.dtGrid.ItemsSource = _river;



                         

                         break;

                    case "Wells":

                      

                         this.dtGrid.Columns.Clear();

                         foreach (DataGridColumn item in this._WellsColumns)
                         {
                             this.dtGrid.Columns.Add(item);
                         }

                         this.dtGrid.ItemsSource = _wells;


                         break;

                    case "Drain":
                         this.dtGrid.Columns.Clear();
                         foreach (DataGridColumn item in this._DrainColumns)
                         {
                             this.dtGrid.Columns.Add(item);
                         }
                         this.dtGrid.ItemsSource = this._Drain;
                         break;
                    case "GeneralHead":
                         this.dtGrid.Columns.Clear();
                         foreach (DataGridColumn item in this._GeneralHeadColumns)
                         {
                             this.dtGrid.Columns.Add(item);
                         }
                         this.dtGrid.ItemsSource = this._GeneralHead;
                         break;


                    case "SpecifiedHead":

                         this.dtGrid.Columns.Clear();


                           foreach (DataGridColumn item in this._SpecifiedheadColumns)
                         {
                             this.dtGrid.Columns.Add(item);
                         }


                         this.dtGrid.ItemsSource = this._specifiedHead;


                         break;
                    default:
                        break;
                }

                
            }


            isVisibleID();

            if (this.dtGrid.Columns.Count > 0)
            {              
                for (int i = 4; i < this.dtGrid.Columns.Count; i++)
                {
                    this.dtGrid.Columns[i].Width = 130;
                }
            }
        }



        private void Button_Click(object sender, RoutedEventArgs e)
        {

            this.ModFlowData.SourceSinks.Rivers.UpdateRivers(this._river.ToList<River>(),this._indices);
            this.ModFlowData.SourceSinks.Drains.UpdateDrains(this._Drain.ToList<Drain>(),this._indices);

            this.ModFlowData.SourceSinks.GeneralHeads.UpdateGHB(this._GeneralHead.ToList<GeneralHead>(),this._indices);

            this.ModFlowData.SourceSinks.Wells.UpdateWells(this._wells.ToList<Well>(),this._indices);

            this.ModFlowData.SourceSinks.SpecifiedHeads.UpdateSpecifiedHeads(this._specifiedHead.ToList<SpecifiedHead>(),this._indices);

            this.Close();

        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnAddBC_Click(object sender, RoutedEventArgs e)
        {

            switch (this._currentBCString)
            {
                case "River":

                    foreach (int item in this._indices)
                    {
                        River river = new River(item, this._nrows, this._ncols, this._nlayers,this._nPeriods);
                        this._river.Add(river);                        
                    }
                    break;
                case "Wells":

                    foreach (int item in this._indices)
                    {
                        Well wel = new Well(item, this._nrows, this._ncols, this._nlayers, this._nPeriods);
                        this._wells.Add(wel);
                    }                  

                    break;
                case "Drain":
                    foreach (int item  in this._indices )
                    {
                        Drain rain = new Drain(item, this._nrows, this._ncols, this._nlayers, this._nPeriods);
                        this._Drain.Add(rain);
                    }
                    break;
                case "GeneralHead":
                    foreach (int item in this._indices)
                    {
                        GeneralHead gen=new GeneralHead (item, this._nrows, this._ncols, this._nlayers, this._nPeriods);
                        this._GeneralHead.Add(gen);
                    }
                    break;


                case "SpecifiedHead":

                    foreach (int item in this._indices)
                    {
                        SpecifiedHead gen = new SpecifiedHead(item, this._nrows, this._ncols, this._nlayers, this._nPeriods);
                        this._specifiedHead.Add(gen);
                    }

                    break;

                default:
                    break;
            }

        }

        private void btnRemoveAll_Click(object sender, RoutedEventArgs e)
        {
            switch (this._currentBCString)
            {
                case "River":
                    this._river.Clear();
                    break;
                case "Wells":
                    this._wells.Clear();
                    break;
                case "Drain":
                    this._Drain.Clear();
                    break;
                case "GeneralHead":
                    this._GeneralHead.Clear();
                    break;


                case "SpecifiedHead":

                    this._specifiedHead.Clear();

                    break;

                default:
                    break;
            }
        }

        private void btnRemoveBC_Click(object sender, RoutedEventArgs e)
        {
            int index = this.dtGrid.SelectedIndex;
            if (index >= 0 && this.dtGrid.Items.Count > 0)
            {
                switch (this._currentBCString)
                {
                    case "River":
                        this._river.RemoveAt(index);
                        break;
                    case "Wells":
                        this._wells.RemoveAt(index);
                        break;
                    case "Drain":
                        this._Drain.RemoveAt(index);
                        break;
                    case "GeneralHead":
                        this._GeneralHead.RemoveAt(index);
                        break;


                    case "SpecifiedHead":

                        this._specifiedHead.RemoveAt(index);

                        break;

                    default:
                        break;
                }
                if (index >= this.dtGrid.Items.Count)
                {
                   index = this.dtGrid.Items.Count - 1;
                }

                this.dtGrid.SelectedIndex = index;
            }
            
        }

        private void Id_Checked(object sender, RoutedEventArgs e)
        {
            isVisibleID();            
        }

        private void isVisibleID()
        {
            if (this.dtGrid == null)
            {
                return;
            }
            if (this.rbtnId.IsChecked.Value)
            {
                var idcol = (from n in this.dtGrid.Columns where n.Header == "ID" select n).First();
                DataGridTextColumn dcId = idcol as DataGridTextColumn;

                if (dcId != null)
                {
                    dcId.Visibility = System.Windows.Visibility.Visible;
                }

                idcol = (from n in this.dtGrid.Columns where n.Header == "行" select n).First();
                dcId = idcol as DataGridTextColumn;
                if (dcId != null)
                {
                    dcId.Visibility = System.Windows.Visibility.Collapsed;
                }

                idcol = (from n in this.dtGrid.Columns where n.Header == "列" select n).First();
                dcId = idcol as DataGridTextColumn;
                if (dcId != null)
                {
                    dcId.Visibility = System.Windows.Visibility.Collapsed;
                }

                idcol = (from n in this.dtGrid.Columns where n.Header == "层" select n).First();
                dcId = idcol as DataGridTextColumn;
                if (dcId != null)
                {
                    dcId.Visibility = System.Windows.Visibility.Collapsed;
                }
            
          
            }
            else
            {

                var idcol = (from n in this.dtGrid.Columns where n.Header == "ID" select n).First();
                DataGridTextColumn dcId = idcol as DataGridTextColumn;

                if (dcId != null)
                {
                    dcId.Visibility = System.Windows.Visibility. Collapsed;
                }

                idcol = (from n in this.dtGrid.Columns where n.Header == "行" select n).First();
                dcId = idcol as DataGridTextColumn;
                if (dcId != null)
                {
                    dcId.Visibility = System.Windows.Visibility.Visible;
                }

                idcol = (from n in this.dtGrid.Columns where n.Header == "列" select n).First();
                dcId = idcol as DataGridTextColumn;
                if (dcId != null)
                {
                    dcId.Visibility = System.Windows.Visibility.Visible;
                }

                idcol = (from n in this.dtGrid.Columns where n.Header == "层" select n).First();
                dcId = idcol as DataGridTextColumn;
                if (dcId != null)
                {
                    dcId.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }
        private void IJK_Checked(object sender, RoutedEventArgs e)
        {

            isVisibleID();
        }
     
    }
}
